using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using pal.SvnConf.Core;

namespace pal.SvnConf.GUI
{
    public class AuthorizationEntriesDataSetView : DataSet, IBindingSource
    {
        public const string AUTHORIZATION_ENTRIES_DATATABLE_NAME = "AuthorizationEntries";
        public const string SECURITY_PRINCIPAL_NAME_COLUMN = "Member Name";
        public const string AUTHORIZATION_COLUMN = "Authorization";
        private readonly List<AuthorizationEntry> _authorizationEntries;
        private DataTable _authorizationEntriesTable;

        public AuthorizationEntriesDataSetView(List<AuthorizationEntry> authorizationEntries)
        {
            _authorizationEntries = authorizationEntries;
            initializeTable();
            setData();
        }

        public BindingSource BindingSource
        {
            get { return new BindingSource(this, AUTHORIZATION_ENTRIES_DATATABLE_NAME); }
        }

        private void setData()
        {
            foreach (AuthorizationEntry authorizationEntry in _authorizationEntries)
                _authorizationEntriesTable.Rows.Add(getRowDataFor(authorizationEntry));
        }

        private static object[] getRowDataFor(AuthorizationEntry user)
        {
            return new object[] {user.SecurityPrincipalName, user.Authorization};
        }

        private void initializeTable()
        {
            _authorizationEntriesTable = new DataTable(AUTHORIZATION_ENTRIES_DATATABLE_NAME);
            setSchema();
            Tables.Add(_authorizationEntriesTable);
        }

        private void setSchema()
        {
            _authorizationEntriesTable.Columns.Add(SECURITY_PRINCIPAL_NAME_COLUMN);
            _authorizationEntriesTable.Columns.Add(AUTHORIZATION_COLUMN);
        }
    }
}